load libraries

#install.packages("gapminder")
#install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(gapminder)

check data

head(gapminder)
## # A tibble: 6 x 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

make scatter plot

ggplot(gapminder, aes(gdpPercap, lifeExp)) +
  geom_point()

log transformation of values

#
ggplot(gapminder, aes(gdpPercap, lifeExp)) +
  geom_point() +
  scale_x_log10()

# try another way
ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point()

change color in scatter plot

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point(aes(color=continent))

#change sige of points
ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point(aes(color=continent), size = 3)

change some other parameters in scatter plot

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point(pch=17, size=2, alpha= .8, aes(color=continent)) 

# what about this
ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point(pch=17, size=2, alpha= .8, color= "red")

use smoothing function

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

specify the method for smoothing

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  geom_smooth(lwd=2, se=FALSE, method="lm", col="blue")

# smooth by continent
ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  geom_smooth(aes(color = continent))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# smooth by continent
ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point(aes(color = continent)) +
  geom_smooth(aes(color = continent))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

example on faceting

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  facet_wrap(~continent)

change faceting by column

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  facet_wrap(~continent, ncol=1)

change faceting by row

ggplot(gapminder, aes(log10(gdpPercap), lifeExp)) +
  geom_point() +
  facet_wrap(~continent, nrow =1)

boxplot and jitter

#boxplot
ggplot(gapminder, aes(continent, lifeExp)) +
  geom_jitter() +
  geom_boxplot()

#alpha
ggplot(gapminder, aes(continent, lifeExp)) +
  geom_jitter(alpha=1/2) +
  geom_boxplot()

#fill color
ggplot(gapminder, aes(continent, lifeExp)) +
  geom_jitter(alpha=1/2) +
  geom_boxplot(aes(fill = continent))

violin and jitter

ggplot(gapminder, aes(continent, lifeExp)) +
  geom_jitter() +
  geom_violin()

#alpha
ggplot(gapminder, aes(continent, lifeExp), fill = continent) +
  geom_jitter(alpha=1/2) +
  geom_violin()

#color
ggplot(gapminder, aes(continent, lifeExp), fill = continent) +
  geom_jitter(alpha=1/2) +
  geom_violin(aes(fill = continent))

histograms

ggplot(gapminder, aes(lifeExp)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# change binsize
ggplot(gapminder, aes(lifeExp)) +
  geom_histogram(bins=50)

#
ggplot(gapminder, aes(lifeExp)) +
  geom_histogram(bins=100)

#
ggplot(gapminder, aes(lifeExp)) +
  geom_histogram(bins=500) 

# color by continent
ggplot(gapminder, aes(lifeExp)) + 
  geom_histogram(aes(color=continent))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#fill
ggplot(gapminder, aes(lifeExp)) + 
  geom_histogram(aes(fill=continent))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

density plot

ggplot(gapminder, aes(lifeExp)) + 
  geom_density()

# color
ggplot(gapminder, aes(lifeExp)) + 
  geom_density(aes(fill= "red"))

change alpha (transparancey)

ggplot(gapminder, aes(lifeExp)) + 
  geom_density(aes(fill= "red"), alpha=1/4)

#
ggplot(gapminder, aes(lifeExp)) + 
  geom_density(aes(fill=continent), alpha=1/4)

density and histogram

ggplot(gapminder, aes(lifeExp)) + 
    geom_density(size=1.5, fill="pink", alpha=0.5) +
    geom_histogram(aes(y=..density..), binwidth=4, color="black", fill="blue", alpha=0.5)

time series line plot

ggplot(gapminder, aes(x=year, y=lifeExp, group=country)) +
    geom_line()

line plot for median life exp

gapminder %>%
    group_by(continent, year) %>%
    summarise(lifeExp=median(lifeExp)) %>%
    ggplot(aes(x=year, y=lifeExp, color=continent)) +
     geom_line(size=1) + 
     geom_point(size=1.5) 

line plot for median life exp and smooth

gapminder %>%
    group_by(continent, year) %>%
    summarise(lifeExp=median(lifeExp)) %>%
    ggplot(aes(x=year, y=lifeExp, color=continent)) +
     geom_line(size=1) + 
     geom_point(size=1.5) +
    geom_smooth(aes(fill=continent), method="lm")